package com.app.teaware.common;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.teaware.bean.ProjectFileData;
import com.app.teaware.bean.ProjectFileListBean;
import com.app.teaware.R;
import com.app.teaware.basis.BasisActivity;
import com.app.teaware.basis.BasisApp;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import my.ActivityTool;
import my.http.MyHttpListener;

/**
 * 查看文件列表
 */
public class ViewFileListActivity extends BasisActivity {

    public static void toFileList(Activity context,String id,String busyType,String title){
        Intent intent = new Intent(context,ViewFileListActivity.class);
        Bundle extras = new Bundle();
        extras.putString("id",id);
        extras.putString("busyType",busyType);
        extras.putString("title",title);
        intent.putExtras(extras);
        context.startActivity(intent);
    }
    public static void toFileList(Activity context, ProjectFileListBean listBean, String title){
        if(listBean == null || listBean.getList().size() == 0){
            BasisApp.showToast("没有相关文件");
            return;
        }
        if(listBean.getList().size() == 1){
            Intent intent = new Intent(context,ViewFileDetailActivity.class);
            Bundle extras = new Bundle();
            extras.putSerializable("data",listBean.getList().get(0));
            extras.putString("title",title);
            intent.putExtras(extras);
            context.startActivity(intent);
        }else{
            Intent intent = new Intent(context,ViewFileListActivity.class);
            Bundle extras = new Bundle();
            extras.putSerializable("list",listBean);
            extras.putString("title",title);
            intent.putExtras(extras);
            context.startActivity(intent);
        }

    }


    @BindView(R.id.recyclerview)
    XRecyclerView mRecyclerView;

    private MyAdapterList mAdapter;
    private ProjectFileListBean mListBean;

    String id;
    String busyType;
    String title;


    @Override
    public void initViews() {
        super.initViews();
        setContentView(R.layout.base_xrecycler_with_top);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setVerticalScrollBarEnabled(true);
        mRecyclerView.setLayoutManager(layoutManager);
//        mRecyclerView.setEmptyView(findViewById(R.id.refresh_view));
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mListBean.refresh();
                getList();
            }

            @Override
            public void onLoadMore() {
                getList();
            }
        });
        mRecyclerView.setPullRefreshEnabled(false);
        mRecyclerView.setLoadingMoreEnabled(false);

        setTitleLeftButton(null);
        setTitle("查看附件");

    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);

        Bundle extras = getIntent().getExtras();

         id = extras.getString("id");
        busyType = extras.getString("busyType");
        title = extras.getString("title");

        mListBean = (ProjectFileListBean) extras.getSerializable("list");

        if(mListBean == null && TextUtils.isEmpty(id)){
            showToast("文件不存在");
            finish();
            return;
        }

        if(mListBean == null){
            mListBean = new ProjectFileListBean();
            setListView();
            getList();
        }else {
//            mRecyclerView.refresh();
            setListView();
        }
        if(!TextUtils.isEmpty(title)){
            setTitle(title);
        }

    }


    private void setListView() {
        if (mAdapter == null) {
            mAdapter = new MyAdapterList();
            mRecyclerView.setAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }
    }

    private void onListViewComplete() {
        mRecyclerView.refreshComplete();
        mRecyclerView.loadMoreComplete();
//        mRecyclerView.setLoadingMoreEnabled(mListBean.hasNextPage());
        mRecyclerView.setLoadingMoreEnabled(false);
        setEmptyView();
    }

    private void setEmptyView() {
        if (mListBean == null || mListBean.getList().size() == 0) {
            findViewById(R.id.refresh_view).setVisibility(View.VISIBLE);
            ((TextView)findViewById(R.id.refresh_text_empty)).setVisibility(View.VISIBLE);
//            ((TextView)findViewById(R.id.refresh_text_empty)).setText("暂无任何信息");
            findViewById(R.id.refresh_view).setOnClickListener(
                    new View.OnClickListener() {
                        public void onClick(View v) {
                            mRecyclerView.refresh();
                        }
                    });
        } else {
            findViewById(R.id.refresh_view).setVisibility(View.INVISIBLE);
        }
    }

    private void getList() {
//        RequestParams params = new RequestParams();
//        showProgress();
//        params.put("busiId", id);
//        params.put("busiTypeCode", busyType);
//
//        MyHttpUtils.post(Constants.URL_COMMENT_GET_ATTACHMENT, params, myHttpListener,
//                HTTP_LIST, ProjectFileListBean.class);

    }

    private final static int HTTP_LIST = 11;
    MyHttpListener myHttpListener = new MyHttpListener() {
        @Override
        public void onSuccess(int httpWhat, Object result) {
            mListBean = (ProjectFileListBean) result;
            setListView();
        }

        @Override
        public void onFailure(int httpWhat, Object result) {
            super.onFailure(httpWhat, result);
//            mListBean = new ServiceNewListBean();
            setListView();
        }

        @Override
        public void onFinish(int httpWhat) {
            hideLoading();
            onListViewComplete();
        }
    };


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == Activity.RESULT_OK){
            mRecyclerView.refresh();
        }
    }

    public class MyAdapterList extends RecyclerView.Adapter<MyAdapterList.ViewHolder> {

        public MyAdapterList() {

        }

        //创建新View，被LayoutManager所调用
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.common_view_file_list_item, viewGroup, false);
            return new ViewHolder(view);
        }


        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int position) {
            viewHolder.bind(position);
        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            return mListBean == null ? 0 : mListBean.getList().size();
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.list_item)
            public View list_item;
            @BindView(R.id.text_name)
            public TextView text_name;
            @BindView(R.id.img_icon)
            public ImageView img_icon;

            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }

            public void bind(int position){
                ProjectFileData data = mListBean.getList().get(position);
//                list_item.setTag(position);
                text_name.setText(data.getFileName());
                img_icon.setImageResource(data.getImgresByname());

                list_item.setTag(position);

            }

            @OnClick(R.id.list_item)
            public void viewDetail(View v) {
                int position =  (int) v.getTag();
                ProjectFileData data = mListBean.getList().get(position);
                Bundle extras = new Bundle();
                extras.putSerializable("data",data);
                extras.putString("title","查看文件");
                ActivityTool.skipActivity( mContext,ViewFileDetailActivity.class,extras);
            }
        }
    }

}
